<template>
  <div>
    <!-- 顶部 -->
    <div :class="['bar', { show: show }]" v-show="!edit">
      <!-- 导航名称显示 -->
      <div class="left navname" @click="back">
        <div class="iconfont icon-fanhui-xianxing">
          <em></em>
        </div>
      </div>
      <div class="middle" v-show="!$route.meta.gender">
        <button :class="{ active: mode }" @click="mode = true">我的书架</button>
        <button :class="{ active: !mode }" @click="mode = false">
          最近阅读
        </button>
      </div>
      <div class="right">
        <div class="menu">
          <span
            class="iconfont icon-icon-7"
            @click="toRouter('search', '')"
          ></span>
          <span :class="{ ins: show }" @click="show = !show"></span>
        </div>
      </div>
    </div>
    <!-- 编辑 -->
    <div class="bar editTop" v-show="edit">
      <!-- 导航名称显示 -->
      <div class="left navname" @click="checkAll">全选</div>
      <div class="middle">共{{ shelf.length }}本</div>
      <div class="right" @click="cancel">取消</div>
    </div>
    <!-- 导航菜单 -->
    <div :class="['menB', { show: show }]" ref="menB">
      <div class="ul">
        <li @click="toRouter('index')">
          <i></i>
          <span>首页</span>
        </li>
        <li @click="toRouter('category')">
          <i></i>
          <span>分类</span>
        </li>
        <li @click="toRouter('rank')">
          <i></i>
          <span>排行榜</span>
        </li>
        <li @click="toRouter('free')">
          <i></i>
          <span>免费</span>
        </li>
        <li @click="toRouter('finished')">
          <i></i>
          <span>完本</span>
        </li>
        <li>
          <i></i>
          <span>账户</span>
        </li>
      </div>
      <div class="bookbtn">我的书架</div>
    </div>
    <!-- 遮罩层 -->
    <div class="mask" v-if="show" @click="show = !show"></div>

    <!-- 内容 -->
    <div class="bookshelfB">
      <!-- 未登录状态 -->
      <div class="nologin" v-if="!$store.state.login">
        <div class="loginBtn">登录</div>
        <p>登录后实时同步进度</p>
      </div>
      <!-- 书架和最近阅读 -->
      <div class="listB">
        <div class="top" v-show="!(edit || !mode)">
          <div class="left">
            <div @click="bookB = true">
              默认书架({{ shelf.length }})<span
                class="iconfont icon-downlist"
              ></span>
            </div>
            <div @click="timeB = true">
              阅读时间 <span class="iconfont icon-paixu"></span>
            </div>
          </div>
          <div class="right" @click="edit = true">编辑</div>
        </div>
        <div class="content">
          <!-- 书架 -->
          <div class="shelf" v-if="mode">
            <van-checkbox-group v-model="result" ref="checkboxGroup">
              <van-cell-group>
                <van-cell
                  v-for="(item, index) in shelf"
                  clickable
                  :key="index"
                  @click.stop="toggle(index)"
                >
                  <li>
                    <div :class="['check', { active: edit }]">
                      <van-checkbox
                        :name="item"
                        ref="checkboxes"
                        checked-color="#ed424b"
                      />
                    </div>
                    <div class="imgs">
                      <img :src="item.cover | imgUrl" alt="" />
                    </div>
                    <div class="text">
                      <div>
                        <h6>{{ item.title }}</h6>
                        <span
                          class="fr"
                          @click.stop="
                            gotoReader('reader', { item: item, id: item._id })
                          "
                          >{{ item.order | readState
                          }}<i class="iconfont icon-i-right"></i
                        ></span>
                      </div>
                      <div>
                        <p>
                          <i class="iconfont icon-icon-12"></i
                          >{{ item.author }} <em>|</em>
                          {{ item.currentTit | readChapter }}
                        </p>
                      </div>
                      <div>
                        <p>更新至 {{ item.lastChapter }}</p>
                        <span class="fr">{{ item.updated | time }}</span>
                      </div>
                    </div>
                  </li>
                </van-cell>
              </van-cell-group>
            </van-checkbox-group>
            <!-- 空状态 -->
            <van-empty description="默认书架是空的" v-if="shelf.length == 0"/>
          </div>
          <!-- 最近阅读 -->
          <div class="lately shelf" v-if="!mode">
            <ul>
              <li
                v-for="(item, index) in lately"
                :key="index"
                @click="gotoReader('reader', { item: item, id: item._id })"
              >
                <div class="imgs">
                  <img :src="item.cover | imgUrl" alt="" />
                </div>
                <div class="text">
                  <div>
                    <h6>{{ item.title }}</h6>
                    <span class="fr"
                      >继续阅读<i class="iconfont icon-i-right"></i
                    ></span>
                  </div>
                  <div>
                    <p>
                      <i class="iconfont icon-icon-12"></i>{{ item.author }}
                    </p>
                  </div>
                  <div>
                    <p>读至 {{ item.currentTit }}</p>
                    <span class="fr">{{ item.lastReadTime | time }}</span>
                  </div>
                </div>
              </li>
            </ul>
            <p class="btmText" v-show="!mode && lately.length != 0">
              以上为全部阅读记录
            </p>
            <p class="btmText" v-show="!mode && lately.length == 0">
              暂无阅读记录
            </p>
          </div>
        </div>
      </div>
      <!-- 选择书架 -->
      <van-popup v-model="bookB" position="bottom">
        <div class="bookgroup">
          <li>
            默认书架({{ shelf.length }})<span
              class="iconfont icon-xuanzhong active"
            ></span>
          </li>
        </div>
      </van-popup>
      <!-- 阅读时间 -->
      <van-popup v-model="timeB" position="bottom">
        <div class="bookgroup">
          <li>阅读时间<span class="iconfont icon-xuanzhong active"></span></li>
          <li>更新时间<span class="iconfont icon-xuanzhong"></span></li>
        </div>
      </van-popup>
      <!-- 删除按钮 -->
      <van-popup
        v-model="edit"
        position="bottom"
        :overlay="false"
        :lock-scroll="false"
      >
        <div class="delB" @click="del">
          <li><span class="iconfont icon-shanchu">删除</span></li>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false, // 顶部导航状态
      bookB: false, // 书架选择
      timeB: false, // 阅读时间选择
      edit: false, //编辑开关
      result: [], // 选中数组
      cheAll: true, // 全选反选
      mode: true, // 我的书架true  最近阅读false
    };
  },
  methods: {
    toggle(index) {
      if (this.edit) {
        this.$refs.checkboxes[index].toggle();
      }
    },
    checkAll() {
      this.cheAll = !this.cheAll;
      this.$refs.checkboxGroup.toggleAll(this.cheAll);
    },
    cancel() {
      this.edit = false;
      this.$refs.checkboxGroup.toggleAll(false);
    },
    toRouter(path, params) {
      this.$router.push({
        path: `/${path}`,
        query: { ...params },
      });
      this.show = false;
    },
    back() {
      this.$router.go(-1);
    },
    gotoReader(path, params) {
      let order = params.item.order || 1;
      this.$router.push({
        path: `/${path}`,
        query: { id: params.id, order: order },
      });
      // console.log(path,params)
      // 更新到最近阅读 当前点击排序到第一
      this.$store.commit("setLately", params.item);
    },
    del() {
      this.$store.commit("delShelf", this.result);
      this.cancel();
    },
  },
  created() {},
  mounted() {
    // 判断是从首页最近阅读按钮进还是其他
    this.mode = this.$route.query.mode == "false" ? false : this.mode;
  },
  computed: {
    lately() {
      // 最近阅读内容
      return this.$store.state.lately;
    },
    shelf() {
      // 书架内容
      return this.$store.state.shelf;
    },
  },
  watch: {
    lately: {
      handler(val) {
        // 监听阅读记录 更新阅读记录
        let shelf = this.shelf;
        shelf.forEach((item, index) => {
          val.some((k) => {
            if (item._id == k._id) {
              // 有阅读记录 更新数据 复合数据类型，不用重新赋值给this.shelf
              shelf.splice(index, 1, k);
            }
          });
        });
      },
      immediate: true,
      deep: true,
    },
  },
  filters: {
    imgUrl(url) {
      return "http://statics.zhuishushenqi.com" + url;
    },
    time(date) {
      let time = null;
      if (typeof date == "string") {
        time =
          new Date().getTime() - new Date((date || "").split(".")[0]).getTime();
      } else {
        time = new Date().getTime() - new Date(date || "").getTime();
      }
      let hour = parseInt(time / 1000 / 60 / 60);
      let day = hour >= 24 ? parseInt(hour / 24) : 0;
      let year = day >= 365 ? parseInt(day / 365) : 0;
      let min = parseInt(time / 1000 / 60);
      let str =
        year != 0
          ? year + "年前"
          : day != 0
          ? day + "天前"
          : hour != 0
          ? hour + "小时前"
          : min != 0
          ? min + "分钟前"
          : "刚刚";
      return str;
    },
    readState(msg) {
      return msg ? "继续阅读" : "立即阅读";
    },
    readChapter(msg) {
      return msg ? "读至 " + msg : "尚未阅读";
    },
  },
};
</script>
<style lang="less" scoped>
.bookshelfB {
  width: 100%;
  height: 100vh;
  background-color: white;
  padding-top: 1.2rem;
  box-sizing: border-box;
  // 未登录状态
  .nologin {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    .loginBtn {
      width: 80%;
      background-color: #ed424b;
      margin: 0 auto;
      font-size: 0.390625rem;
      color: white;
      text-align: center;
      line-height: 0.9375rem;
      border-radius: 0.9375rem;
    }
    p {
      font-size: 0.390625rem;
      text-align: center;
      margin: 0.3125rem 0;
      color: #aaa;
    }
  }
  .listB {
    .top {
      display: flex;
      justify-content: space-between;
      font-size: 0.36rem;
      padding: 0.390625rem;
      .left {
        div {
          display: inline-block;
          &:nth-child(1) {
            font-weight: bold;
            margin-right: 0.3125rem;
          }
          &:nth-child(2) {
            color: cornflowerblue;
          }
        }
      }
    }
    .content {
      margin-bottom: 1rem;
      .shelf {
        padding-left: 0.390625rem;
        li {
          display: flex;
          padding: 0.2rem 0;

          .check {
            flex: 0 0 0rem;
            position: relative;
            transition: all 0.2s;
            overflow: hidden;
            &.active {
              flex: 0 0 0.8rem;
            }
            .van-checkbox {
              position: absolute;
              top: 50%;
              transform: translateY(-50%);
            }
          }
          .imgs {
            flex: 0 0 1.5rem;
            img {
              width: 100%;
              height: auto;
              vertical-align: top;
            }
          }
          .text {
            flex: 1;
            padding: 0 0.3rem;
            box-sizing: border-box;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            & > div {
              width: 100%;
              height: 0.6rem;
              font-size: 0;
              h6 {
                height: 100%;
                line-height: 0.6rem;
                font-size: 0.39rem;
                display: inline-block;
              }
              span {
                height: 100%;
                line-height: 0.6rem;
                font-size: 0.36rem;
                color: #999;
                &.fr {
                  float: right;
                }
                i {
                  font-size: 0.36rem;
                }
              }
              p {
                height: 100%;
                line-height: 0.6rem;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                font-size: 0.36rem;
                color: #999;
                em {
                  font-size: 0.39rem;
                  margin: 0 0.15625rem;
                  color: #999;
                }
              }
              &:nth-child(3) {
                position: relative;
                &::before {
                  content: "";
                  width: 0.1rem;
                  height: 0.1rem;
                  background-color: red;
                  position: absolute;
                  left: -0.16rem;
                  top: 0.1rem;
                  border-radius: 50%;
                }
                p {
                  width: 70%;
                  float: left;
                }
              }
            }
          }
        }
      }
      .lately {
        padding-bottom: 0.71875rem;
        li {
          border-bottom: 1px solid #ddd;
          &:active {
            background-color: #f2f2f2;
          }
          .text {
            & > div {
              &::before {
                content: "";
                background-color: white !important;
              }
            }
          }
        }
        .btmText {
          color: #aaa;
          font-size: 0.390625rem;
          text-align: center;
          margin-top: 0.71875rem;
        }
      }
    }
  }
  .bookgroup {
    li {
      font-size: 0.359375rem;
      padding: 0.234375rem 0.3125rem;
      span {
        float: right;
        color: #ee424b;
        display: none;
        &.active {
          display: inline;
        }
      }
    }
  }
  .delB {
    height: 1rem;
    border-top: 1px solid rgba(153, 153, 153, 0.459);
    box-sizing: border-box;
    li {
      font-size: 0.390625rem;
      color: tomato;
      text-align: center;
      line-height: 1rem;
    }
  }
}

// 顶部
.bar {
  width: 100%;
  height: 1.2rem;
  display: flex;
  justify-content: space-between;
  padding: 0.25rem;
  position: absolute;
  box-sizing: border-box;
  z-index: 4;
  border-bottom: 1px solid #ddd;
  &.show {
    background-color: white;
  }
  .left {
    width: 2.4rem;
    margin-left: 0.15625rem;
    flex: 1;
    &.init {
      background: url("../assets/images/logo-qdm.02fc8.svg") no-repeat center;
      background-size: 100%;
    }
    &.navname {
      font-size: 0;
      div {
        height: 100%;
        line-height: 0.7rem;
        font-size: 0.45rem;
        em {
          font-style: normal;
          font-size: 0.38rem;
          margin-left: 0.304375rem;
          vertical-align: top;
          line-height: 0.65rem;
        }
      }
    }
  }
  .middle {
    font-size: 0;
    flex: 2;
    text-align: center;
    button {
      font-size: 0.4rem;
      border: 1px solid #ed424b;
      color: #ed424b;
      background-color: white;
      padding: 0.048125rem 0.3125rem;
      height: 100%;
      &:nth-child(1) {
        border-radius: 4px 0 0 4px;
      }
      &:nth-child(2) {
        border-radius: 0 4px 4px 0;
      }
      &.active {
        background-color: #ed424b;
        color: white;
      }
    }
  }
  .right {
    font-size: 0;
    flex: 0 0 2.28125rem;
    span {
      position: relative;
      font-size: 0.625rem;
      color: #ed424b;
      margin-left: 0.46875rem;
      &:nth-child(2) {
        font-size: 0.5rem;
      }
    }
    .menu {
      height: 100%;
      line-height: 0.65rem;
      span {
        color: #000;
        &:nth-child(1) {
          font-size: 0.525rem;
          margin-left: 0.51875rem;
        }
        &:nth-child(2) {
          top: 0.12rem;
          width: 0.46875rem;
          height: 0.46875rem;
          display: inline-block;
          &::before {
            content: "";
            position: absolute;
            width: 0.4375rem;
            height: 0.099rem;
            border-top: 1px solid black;
            border-bottom: 1px solid black;
            left: 0;
            top: 0.12rem;
            transition: all 0.5s;
          }
          &::after {
            content: "";
            position: absolute;
            width: 0.4375rem;
            border-top: 1px solid black;
            top: 0;
            transition: all 0.2s;
          }
          // 变插动画
          &.ins {
            &::before {
              border-bottom: none;
              transform: rotate(45deg);
              height: 0;
            }
            &::after {
              transform: translateY(0.09rem) rotate(-45deg);
            }
          }
        }
      }
    }
  }
}
.menB {
  position: fixed;
  top: 1.2rem;
  z-index: 3;
  width: 100%;
  height: 0px;
  background-color: white;
  overflow: hidden;
  padding-bottom: 0;
  transition: height 0.2s;
  &.show {
    height: 6rem;
    padding-bottom: 0.625rem;
  }
  .ul {
    display: flex;
    flex-wrap: wrap;
    li {
      width: 33.33%;
      height: 2.34375rem;
      padding: 0.46875rem 0;
      text-align: center;
      box-sizing: border-box;
      i {
        width: 26px;
        height: 26px;
        display: block;
        margin: 0 auto 0.15625rem;
        background-image: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
      }
      &:nth-child(1) {
        i {
          background-position: -31px 0;
        }
      }
      &:nth-child(2) {
        i {
          background-position: -61px -28px;
        }
      }
      &:nth-child(4) {
        i {
          background-position: -0px -30px;
        }
      }
      &:nth-child(5) {
        i {
          background-position: 1px -61px;
        }
      }
      &:nth-child(6) {
        i {
          background-position: -61px 0px;
        }
      }
      span {
        font-size: 0.359375rem;
        font-weight: bold;
        vertical-align: top;
      }
    }
  }
  .bookbtn {
    width: 80%;
    height: 0.9375rem;
    background-color: #ed424b;
    color: white;
    line-height: 0.9375rem;
    border-radius: 0.9375rem;
    text-align: center;
    font-size: 0.359375rem;
    margin: 0.3125rem auto 0;
  }
}
.mask {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #00000085;
  transition: all 0.2s;
  z-index: 2;
}
// 编辑顶部
.editTop {
  div.left.navname {
    font-size: 0.390625rem;
    line-height: 0.7rem;
  }
  div.middle {
    font-size: 0.390625rem;
    line-height: 0.7rem;
  }
  div.right {
    font-size: 0.390625rem;
    line-height: 0.7rem;
    text-align: right;
    margin-right: 0.15625rem;
  }
}
</style>